function init(){
    initTables();
    ajaxCall("GET",path+"/GroupServlet?method=getUsers",setInit);
}
function initTables() {
    ajaxCall("GET",path+"/GroupServlet?method=getGroups",setGroupsTable);
}

function add(){
    var select1=document.getElementById("s1");
    var userName1=select1.value;
    var select2=document.getElementById("s2");
    var userName2=select2.value;
    ajaxCall("Post",path+"/GroupServlet?userName1="+userName1+"&userName2="+userName2,addResult);
}
function addResult(res) {
    var obj=JSON.parse(res);
    if(obj){
        initTables();
        alert("添加成功");
    }
    else{
        alert("添加失败");
    }
}
function setInit(res){
    var select1=document.getElementById("s1");
    var select2=document.getElementById("s2");
    var obj=JSON.parse(res);
    for(var i in obj){
        select1.options.add(new Option(obj[i].name,obj[i].name));
        select2.options.add(new Option(obj[i].name,obj[i].name));
    }
}
function setGroupsTable(res) {
    var obj=JSON.parse(res);
    var table=document.getElementById("t1");
    clearTable("t1");
    for(var i in obj){
        var newRow=table.insertRow(1);
        var groupId=newRow.insertCell(0);
        var userName1=newRow.insertCell(1);
        var userName2=newRow.insertCell(2);
        var deletes=newRow.insertCell(3);

        groupId.innerHTML=obj[i].id;
        userName1.innerHTML=obj[i].userName1;
        userName2.innerHTML=obj[i].userName2;
        deletes.innerHTML="<input type='button' value='删除' onclick='deletes(this)'/>";
    }
}
function clearTable(tableName) {
    var tb = document.getElementById(tableName);
    var rowNum = tb.rows.length;
    for (i = 1; i < rowNum; i++) {
        tb.deleteRow(i);
        rowNum = rowNum - 1;
        i = i - 1;
    }
}
function deletes(row){
    var rowIndex=row.parentNode.parentNode.rowIndex;
    var table=document.getElementById("t1");
    var groupId=table.rows[rowIndex].cells[0].innerHTML;
    ajaxCall("GET",path+"/GroupServlet?method=deletes&groupId="+groupId,deletesResult);
}
function deletesResult(res) {
    var obj=JSON.parse(res);
    if(obj){
        initTables();
        alert("删除成功！")
    }
    else{
        alert("删除失败！");
    }
}






